package com.lepai.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lepai.bean.BankPay;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author weihaolong
 */
@RestController
@RequestMapping("/restful")
public class RestfulController {

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/pay")
    public BankPay test(@RequestBody BankPay bankPay){
        return bankPay;
    }

    @GetMapping("/rest")
    public void rest() throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(buildBankPay());

        HttpEntity<String> formEntity = new HttpEntity<>(json, headers);
        restTemplate.postForEntity("http://localhost:8080/test/pay", formEntity, String.class);
    }

    @GetMapping("/rest2")
    public void rest2() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(buildBankPay());

        StringEntity myEntity = new StringEntity(json,
                ContentType.create("application/json", "UTF-8"));

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://localhost:8080/test/pay");
        httpPost.setEntity(myEntity);
        CloseableHttpResponse response = httpclient.execute(httpPost);
    }

    /**
     * Http请求
     */
    @GetMapping
    public void httpGet(){
        HttpGet httpget = new HttpGet("http://localhost:8080/test/rest");
        httpget.getMethod();
    }

    private BankPay buildBankPay(){
        BankPay bankPay = new BankPay();
        bankPay.setCharset("UFT-8");
        bankPay.setVersion("1.1");
        bankPay.setSignType("SHA-256");
        bankPay.setSign("sadfda");
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("dateTime", "20160623101430");
        hashMap.put("txCode", "FBPK");
        hashMap.put("branchNo", "1234");
        hashMap.put("merchantNo", "666666");
        bankPay.setReqData(hashMap);
        return bankPay;
    }
}
